package com.zhongshengnetwork.rightbe.common.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ReportModel;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//三检查OK:2016-11-30
public class ReportActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private TopBarView topbar;
    private ListView listview;
    private int reportType;
    private String reportId;
    private List<ReportModel> dataList;
    private MyAdapter adapter;
    private int selectIndex = -1;
    private String otherText = "";
    private int mCurrentTouchedIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        reportType = getIntent().getIntExtra(APIKey.typeKey,0);
        reportId = getIntent().getStringExtra(APIKey.idKey);
        setContentView(R.layout.activity_report);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.getRightTextView().setVisibility(View.VISIBLE);
        topbar.getRightTextView().setText("提交");
        topbar.getRightTextView().setTextSize(16.0f);
        topbar.getRightTextView().setTextColor(getResources().getColor(R.color.defaultLightColor));
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectIndex >= 0){
                   final Map<String,String> map = new HashMap<String, String>();
                    map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                    map.put("reportid",reportId);
                    map.put(APIKey.typeKey,reportType+"");
                    map.put("way",selectIndex+"");
                    map.put(APIKey.contentKey, CommonUtils.formatString(otherText));
                    HttpsUtils.reportDo(map, new HttpCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            CommonModel commonModel = GsonTools.getCommonModel(data);
                            if (commonModel.getFlag().equals("1")){
                                Toast.makeText(ReportActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                CustomApplication.showTip(commonModel,ReportActivity.this);
                            }
                        }

                        @Override
                        public void onError(String msg) {

                        }
                    });
                }
            }
        });
        /*
        Resources resources=getBaseContext().getResources();
        Drawable drawable=resources.getDrawable(R.color.commonColor);
        topbar.getRightTextView().setBackgroundDrawable(drawable);
        */
        topbar.setClickListener(this);
        dataList = new ArrayList<ReportModel>();
        for (int i = 0; i < 8; i++){
            ReportModel model = new ReportModel();
            model.setSelectState(false);
            if (i == 0){
                model.setName("色情低俗");
            }else if (i == 1){
                model.setName("广告骚扰");
            }else if (i == 2){
                model.setName("政治敏感");
            }else if (i == 3){
                model.setName("谣言");
            }else if (i == 4){
                model.setName("骗钱");
            }else if (i == 5){
                model.setName("违法（暴力恐怖、违禁品等）");
            }else if (i == 6){
                model.setName("侵权举报（诽谤、抄袭、冒用...）");
            }else if (i == 7){
                model.setName("其他");
            }
            dataList.add(model);
        }
        listview=(ListView) findViewById(R.id.report_listview);
        adapter=new MyAdapter();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (selectIndex == -1){
                    topbar.getRightTextView().setTextColor(getResources().getColor(R.color.mainColor));
                }
                selectIndex = position;
                adapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return dataList.size()+3;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view=convertView;
            if (position == 0) {
                view = LayoutInflater.from(ReportActivity.this).inflate(R.layout.report_listview_header, null);
                TextView title = (TextView) view.findViewById(R.id.report_listview_header_title);
                title.setText("请选择举报原因");
            } else if(position <= dataList.size()){
                ReportModel model = dataList.get(position-1);
                view = LayoutInflater.from(ReportActivity.this).inflate(R.layout.report_listview_item, null);
                ImageView icon = (ImageView) view.findViewById(R.id.report_listview_item_icon);
                TextView name = (TextView) view.findViewById(R.id.report_listview_item_text);
                name.setText(model.getName());
                if (selectIndex == position){
                    icon.setImageResource(R.drawable.selecting);
                }else {
                    icon.setImageResource(R.drawable.unselect);
                }
            }else if (position == dataList.size() + 1){
                view = LayoutInflater.from(ReportActivity.this).inflate(R.layout.report_listview_header, null);
                TextView title = (TextView) view.findViewById(R.id.report_listview_header_title);
                title.setText("请举证（选填）");
            }else {
                view = LayoutInflater.from(ReportActivity.this).inflate(R.layout.report_listview_content, null);
                EditText editText = (EditText) view.findViewById(R.id.report_listview_content_edit);
                editText.addTextChangedListener(new Watcher(editText));
                editText.setOnTouchListener(new OnEditTextTouched(position));
                editText.clearFocus();
                if (position == mCurrentTouchedIndex) {
                    // 如果该项中的EditText是要获取焦点的
                    editText.requestFocus();
                }
                editText.setText(otherText);
            }
            return view;
        }

    }

    // ListView中EditText的触摸事件
    private class OnEditTextTouched implements View.OnTouchListener {
        private int position;

        public OnEditTextTouched(int position) {
            this.position = position;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                mCurrentTouchedIndex = position;
            }
            return false;
        }
    }

    public class Watcher implements TextWatcher
    {
        private EditText EditID = null;
        public Watcher(EditText id){
            EditID = id;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            otherText = EditID.getText().toString().trim();
        }

    }

}
